package com.sky.edumanageapi.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sky.edumanageapi.course.mapper.CourseMapper;
import com.sky.edumanageapi.course.pojo.entity.Course;
import com.sky.edumanageapi.order.mapper.OrderMapper;
import com.sky.edumanageapi.order.pojo.dto.OrderQuery;
import com.sky.edumanageapi.order.pojo.dto.OrderSaveParam;
import com.sky.edumanageapi.order.pojo.entity.Order;
import com.sky.edumanageapi.order.pojo.vo.OrderVO;
import com.sky.edumanageapi.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class OrderServiceImpl  implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Order> selectAll(OrderQuery orderQuery) {
        log.debug("查询订单所有的数据:{}", orderQuery);
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        List<Order> orders = orderMapper.selectList(orderQueryWrapper);
        return orders;
    }

    @Override
    public void save(OrderSaveParam orderSaveParam) {
        log.debug("新增一条订单的数据:{}", orderSaveParam);
        Order order = new Order();
        BeanUtils.copyProperties(orderSaveParam, order);
        if(order.getOrderId()==null){
            order.setCreateTime(new Date());
            order.setStatus(0);//新增的订单目前是未支付状态
            System.out.println(order);
            orderMapper.insert(order);

        }else {
            order.setUpdateTime(new Date());
            QueryWrapper<Order> objectQueryWrapper = new QueryWrapper<>();
            orderMapper.update(order,objectQueryWrapper);
        }
    }

    @Override
    public void update(Integer orderId) {
        log.debug("修改订单的状态为已取消:{}", orderId);//通过点击删除按钮会将订单改变为已支付状态
        UpdateWrapper<Order> objectQueryWrapper = new UpdateWrapper<>();
        objectQueryWrapper.set("status", 1);
        objectQueryWrapper.set("update_time", new Date());
        objectQueryWrapper.eq("order_id", orderId);
        orderMapper.update(null,objectQueryWrapper);
    }

    @Override
    public void delete(@Param("orderId") Integer orderId) {
        log.debug("修改订单的状态为已取消:{}", orderId);//通过点击删除按钮会将订单改变为已取消状态
        UpdateWrapper<Order> objectQueryWrapper = new UpdateWrapper<>();
        objectQueryWrapper.set("status", 2);
        objectQueryWrapper.set("update_time", new Date());
        objectQueryWrapper.eq("order_id", orderId);
        orderMapper.update(null,objectQueryWrapper);
    }

    @Override
    public Order selectByOrderId(Integer orderId) {
        log.debug("根据订单号查询订单详情:{}", orderId);
        QueryWrapper<Order> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("order_id", orderId);
        Order order = orderMapper.selectOne(objectQueryWrapper);
        return order;
    }




}
